package com.ruoyi.web.controller.drug;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.drug.domain.DrugPhysics;
import com.ruoyi.drug.domain.DrugRecipt;
import com.ruoyi.drug.domain.DrugReciptPhysics;
import com.ruoyi.drug.service.IDrugReciptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/drug/recipt")
public class DrugReciptController extends BaseController {
    @Autowired
    private IDrugReciptService drugReciptService;

    @GetMapping("/getUserName")
    public AjaxResult getUserName(){
        return success(getUsername());
    }

    @PreAuthorize("@ss.hasPermi('drug:recipt:listStore')")
    @GetMapping("/listStore")
    public TableDataInfo storeList(DrugPhysics drugPhysics){
        startPage();
        List<DrugPhysics> list = drugReciptService.selectPhysicsList(drugPhysics);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('drug:reciptPhysic:edit')")
    @Log(title="添加药品",businessType = BusinessType.INSERT)
    @PutMapping("/{ids}")
    public AjaxResult addReciptPhysic(@PathVariable Long[] ids,@Validated @RequestBody String reciptNum){
        return toAjax(drugReciptService.addReciptPhysic(ids,reciptNum));
    }

    @PreAuthorize("@ss.hasPermi('drug:recipt:listStore')")
    @GetMapping("/list")
    public TableDataInfo reciptPhysicsList(String reciptNum){
        startPage();
        List<DrugReciptPhysics> list = drugReciptService.selectReciptPhysics(reciptNum);
        return getDataTable(list);
    }

    @Log(title="暂存单据",businessType = BusinessType.INSERT)
    @PostMapping("/transient")
    public AjaxResult addRecipt(@Validated @RequestBody DrugRecipt drugRecipt){
        int result = drugReciptService.addRecipt(drugRecipt);
        if (result==0){
            return error("请选择供应商和制单时间");
        }
        return toAjax(result);
    }

    @Log(title="提交审核",businessType = BusinessType.INSERT)
    @PostMapping("/process")
    public AjaxResult addRecipt1(@Validated @RequestBody DrugRecipt drugRecipt){
        int result = drugReciptService.addRecipt1(drugRecipt);
        if (result==0){
            return error("请选择供应商和制单时间");
        }
        return toAjax(result);
    }

    @PreAuthorize("@ss.hasPermi('drug:recipt:list')")
    @GetMapping("/listRecipt")
    public TableDataInfo reciptList(DrugRecipt drugRecipt){
        startPage();
        List<DrugRecipt> list = drugReciptService.selectRecipt(drugRecipt);
        return getDataTable(list);
    }

    @Log(title="批量设置药品",businessType = BusinessType.INSERT)
    @PutMapping("/setting/{ids}")
    public AjaxResult updateSetting(@PathVariable Long[] ids,@Validated @RequestBody DrugReciptPhysics drugReciptPhysics){
       return toAjax(drugReciptService.updateSetting(ids,drugReciptPhysics));
    }

    @Log(title="删除药品",businessType = BusinessType.DELETE)
    @DeleteMapping("/physic/{physicId}")
    public AjaxResult removePhysic(@PathVariable Long physicId){
        return toAjax(drugReciptService.deletePhysic(physicId));
    }

    @Log(title="逻辑删除单据",businessType = BusinessType.UPDATE)
    @PutMapping("/delRecipt/{ids}")
    public AjaxResult removeRecipt(@PathVariable Long[] ids){
        return toAjax(drugReciptService.deleteRecipt(ids));
    }

    @Log(title="批量提交审核",businessType = BusinessType.UPDATE)
    @PutMapping("/processRecipt/{ids}")
    public AjaxResult processRecipt(@PathVariable Long[] ids){
        return toAjax(drugReciptService.processRecipt(ids));
    }


    @Log(title="入库",businessType = BusinessType.UPDATE)
    @PutMapping("/reciptPutIn/{ids}")
    public AjaxResult reciptPutIn(@PathVariable Long[] ids){
        String username = getUsername();
        drugReciptService.reciptPutIn(ids[0],username);
        return success("入库成功，新药品入库还需去药品信息维护中设置相关内容");
    }

    @GetMapping("/selectReciptByNum")
    public AjaxResult selectReciptByNum(DrugRecipt drugRecipt){
        return success(drugReciptService.selectReciptByRNum(drugRecipt.getReciptNum()));
    }
}
